<?php
class ApiController extends CController{
	
/*
	 *数据接口方法
	 *@chenqiwen
	 *$Tablename数据库表的Model名称
	 *$Key_value_pairs键值对数组
	 */
	public function Data_interface_method($Tablename='',$Key_value_pairs=array()){
		$key = '0d291452bc581a021494f7b4bdbb3d5d';
		$apptk = isset($_GET['apptk']) ? $_GET['apptk']:'';
		$limit = isset($_GET['number']) ? $_GET['number']:10;
		$order = isset($_GET['order']) ? $_GET['order']:'';
		$page = isset($_GET['lastID']) ? $_GET['lastID']:1;
		if ($apptk !='' && $key == $apptk){
			if (empty($Key_value_pairs))
			die('字段匹配有误');
			$criteria = new CDbCriteria();
			if ($limit != '')
			$criteria->limit = $limit;
			if ($order != '')
			$criteria->order ='id '.$order;
			$model = new CActiveDataProvider($Tablename, array(
				'criteria'=>$criteria,
				'pagination'=>$page,
			));
			
			if (!empty($model)){
				$array = array();
				$array['code'] = 1;
				foreach ($model as $value){
					$arr = array();
					foreach ($Key_value_pairs as $key=>$val) {
						$arr[$key] = CHtml::value($value, $val);
					}
					$array['data'][] = $arr;
				}
				echo json_encode($array);
				
			}else {
				$err = array(
					'isok'=>0,
					'msg' =>'数据表数据为空！',
				);
				echo json_encode($err);
			}
		}else {
			$error = array(
				'isok'=>0,
				'msg' =>'秘钥有误',
			);
			echo json_encode($error);
			
		}
	}
}
?>